package com.example.mideaoem.data;

import com.example.mideaoem.data.dehumidificator.DataBodyDeHumiQuery;
import com.example.mideaoem.data.dehumidificator.DataBodyDeHumiRequest;
import com.example.mideaoem.data.dehumidificator.DataBodyDeHumiResponse;
import com.example.mideaoem.data.humidification.DataBodyHumiQuery;
import com.example.mideaoem.data.humidification.DataBodyHumiRequest;
import com.example.mideaoem.data.humidification.DataBodyHumiResponse;

import android.util.Log;


public abstract class FactoryDataBody implements DataBodyDevInterface{

	public final static int NEW = 1;
	public final static int OLD = 2;
	
	public final static int AirConditioner = 0;//空调
	public final static int Dehumidifier  = 1;//除湿机
	public final static int Humidifier = 2;//加湿器
	
	
	public static byte getProtocol() {
		return protocol;
	}

	public static void setProtocol(byte protocol) {
		FactoryDataBody.protocol = protocol;
	}

	private static byte protocol = (byte) 0x03;
	
	protected static byte mOrder = 10;//加湿，除湿用
	public byte[] toBytes(int[] src, int length){
		byte[] result = new byte[length];
		for(int i =0;i<result.length;i++){
			result[i] = (byte)src[i];
		}
		return result;
	}
	
	
	// CRC8校验数据表 X^8 + X^5 + X^4 + 1
	public static int[] crc8_854_table = 
		{ 0, 94, 188, 226, 97, 63, 221, 131,
			194, 156, 126, 32, 163, 253, 31, 65, 157, 195, 33, 127, 252, 162,
			64, 30, 95, 1, 227, 189, 62, 96, 130, 220, 35, 125, 159, 193, 66,
			28, 254, 160, 225, 191, 93, 3, 128, 222, 60, 98, 190, 224, 2, 92,
			223, 129, 99, 61, 124, 34, 192, 158, 29, 67, 161, 255, 70, 24, 250,
			164, 39, 121, 155, 197, 132, 218, 56, 102, 229, 187, 89, 7, 219,
			133, 103, 57, 186, 228, 6, 88, 25, 71, 165, 251, 120, 38, 196, 154,
			101, 59, 217, 135, 4, 90, 184, 230, 167, 249, 27, 69, 198, 152,
			122, 36, 248, 166, 68, 26, 153, 199, 37, 123, 58, 100, 134, 216,
			91, 5, 231, 185, 140, 210, 48, 110, 237, 179, 81, 15, 78, 16, 242,
			172, 47, 113, 147, 205, 17, 79, 173, 243, 112, 46, 204, 146, 211,
			141, 111, 49, 178, 236, 14, 80, 175, 241, 19, 77, 206, 144, 114,
			44, 109, 51, 209, 143, 12, 82, 176, 238, 50, 108, 142, 208, 83, 13,
			239, 177, 240, 174, 76, 18, 145, 207, 45, 115, 202, 148, 118, 40,
			171, 245, 23, 73, 8, 86, 180, 234, 105, 55, 213, 139, 87, 9, 235,
			181, 54, 104, 138, 212, 149, 203, 41, 119, 244, 170, 72, 22, 233,
			183, 85, 11, 136, 214, 52, 106, 43, 117, 151, 201, 74, 20, 246,
			168, 116, 42, 200, 150, 21, 75, 169, 247, 182, 232, 10, 84, 215,
			137, 107, 53 };
	
	
	/**
	 * 
	 * @param dataBuf
	 * @param dataLen 不包含自己，传dataBuf.length-1
	 * @return
	 */
	public static int getCRC(byte[] dataBuf, int dataLen )
	{
		int crc = 0;

		if ( null == dataBuf || 0 == dataLen )
		{
			return crc;
		}
		byte[] tmp = dataBuf;
		int si = 0;
		for(si = 0; si< dataLen; si++)
		{
			int index = crc ^ (tmp[si]);
			if(index > 256){
				index -= 256;
			}
			if(index < 0){
				index += 256;
			}
			crc = crc8_854_table[index];
		}
		return crc;
	}
	
	public static String printHexString(byte[] b)    
    {
		StringBuilder sb = new StringBuilder();
        for (int i = 0; i < b.length; i++)
        {
        	//HEX String
            String hex = Integer.toHexString(b[i] & 0xFF);  
            if (hex.length() == 1)
            {
                hex = '0' + hex;
            }
            sb.append(hex+",");
        }
        return sb.toString();
    } 
	
	public static String printHexString(byte b)    
    {
		StringBuilder sb = new StringBuilder();
            String hex = Integer.toHexString(b & 0xFF);  
            if (hex.length() == 1)
            {
                hex = '0' + hex;
            }
            sb.append(hex+",");

        return sb.toString();
    }
	
	
	 public static int unsignedByteToInt(byte b) {
		    return (int) b & 0xFF;
		  }
	
	public static byte makeSum(byte[] bytes, int tmpLen) {
		
		byte resVal = 0;
		int si;
		for (si = 1; si < tmpLen; si++) {
			resVal = (byte) (resVal + bytes[si]);
		}
		resVal = (byte) ((255 - (resVal % 256)) + 1);
		
		Log.d("CHECKSUM","生成校验和  长度="+tmpLen+"   "+printHexString(bytes)+"   "+resVal);
		
		return resVal;

	}
	
	private byte[] AChead = new byte[]{(byte)0xAA, 0X00, (byte)0XAC, 0X00, 0X00, 0X00, 0X00, 0X00, 0X03, 0X02};
	
	/**
	 * 设置协议版本
	 * @param protocol 0x00 0x01 0x02 0x03
	 */
	public void updateProtocol(byte protocol){
		AChead[8] = protocol;
	}
	
	/**
	 * 添加头
	 * @param bytes
	 * @return
	 */
	public byte[] addHead(byte[] bytes){
		return addHead(bytes, (byte)0xAC);
	}
	
	/**添加头 with 设备类型*/
	public byte[] addHead(byte[] bytes, byte deivceType){
		byte[] result = new byte[bytes.length+AChead.length+1];
		AChead[1] = (byte)(result.length-1);									//长度
		AChead[2] = deivceType;
		AChead[AChead.length - 1] = (byte) 0x02;
		System.arraycopy(AChead, 0, result, 0, AChead.length);					
		System.arraycopy(bytes, 0, result, AChead.length, bytes.length);		//组合byte
		result[result.length-1] = makeSum(result, bytes.length+AChead.length);	//消息校验码
		return result;
	}
	
	/**添加头 with 设备类型*/
	public byte[] addHead(byte[] bytes, byte deivceType, boolean isQuery){
		byte[] result = new byte[bytes.length+AChead.length+1];
		AChead[1] = (byte)(result.length-1);									//长度
		AChead[2] = deivceType;
		if(isQuery){
			AChead[AChead.length - 1] = (byte) 0x03;
		}else{
			AChead[AChead.length - 1] = (byte) 0x02;
		}
		System.arraycopy(AChead, 0, result, 0, AChead.length);					
		System.arraycopy(bytes, 0, result, AChead.length, bytes.length);		//组合byte
		result[result.length-1] = makeSum(result, bytes.length+AChead.length);	//消息校验码
		return result;
	}
	
	protected byte[] addQueryHead(byte[] bytes){
		return addCustomHead(bytes, (byte)0x03);
	}
	
	protected byte[] addQueryB5Header(byte[] bytes, byte byteType){
		return addCustomHead(bytes, (byte) 0x03, byteType);
	}
	
	protected byte[] addTimeHead(byte[] bytes){
		return addCustomHead(bytes, (byte)0x04);
	}
	
	
	
	private byte[] addCustomHead(byte[] bytes, byte byteType, byte deviceType){
		byte[] result = new byte[bytes.length+AChead.length+1];
		AChead[1] = (byte)(result.length-1);									//长度
		AChead[AChead.length-1] = (byte)byteType;
		AChead[2] = deviceType;
		System.arraycopy(AChead, 0, result, 0, AChead.length);					
		System.arraycopy(bytes, 0, result, AChead.length, bytes.length);		//组合byte
		result[result.length-1] = makeSum(result, bytes.length+AChead.length);	//消息校验码
		return result;
	}
	
	private byte[] addCustomHead(byte[] bytes, byte byteType){
		byte[] result = new byte[bytes.length+AChead.length+1];
		AChead[1] = (byte)(result.length-1);									//长度
		AChead[AChead.length-1] = (byte)byteType;
		System.arraycopy(AChead, 0, result, 0, AChead.length);					
		System.arraycopy(bytes, 0, result, AChead.length, bytes.length);		//组合byte
		result[result.length-1] = makeSum(result, bytes.length+AChead.length);	//消息校验码
		return result;
	}
	
	/**
	 * 解析返回的数值
	 * @param hexString
	 * @return
	 */
	public static byte[] hexStringToBytes(String hexString) {
        if (hexString == null || hexString.equals("")) {
            return null;
        }
        hexString = hexString.toUpperCase();
        int length = hexString.length() / 2;
        char[] hexChars = hexString.toCharArray();
        byte[] d = new byte[length];
        for (int i = 0; i < length; i++) {
            int pos = i * 2;
            d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
        }
        return d;
    }
    private static byte charToByte(char c) {
        return (byte) "0123456789ABCDEF".indexOf(c);
    }
	
	public static FactoryDataBody createFactoryDataBody(int type){
			return new DataBodyDevOld(type);
	}
	
	public static FactoryDataBody createNewDataBodyDev(int type){
		return new DataBodyDevNew(type);
	}

	public static DataBodyDevInterface createFactoryDataBodyResponse() {
		// TODO Auto-generated method stub
		return new DataResponseOld();
	}
	
	/**
	 * 查询B5命令
	 * @return
	 */
	public static DataBodyDevInterface createQueryB5Request(){
		return new DataBodyQueryBFive();
	}
	
	public static DataBodyDevInterface createQueryB5Request(byte type){
		return new DataBodyQueryBFive(type);
	}
	
	
	/**
	 * 查询空调状态
	 * @return
	 */
	public static DataBodyDevInterface createQueryRequest(){
		return new DataBodyDevQuery();
	}
	
	/**
	 * 查询内风机运行时间
	 * @return
	 */
	public static DataBodyDevInterface createQueryTimeRequest(){
		return new DataBodyQueryTime();
	}
	
	public static DataBodyDevInterface createQueryPowerRequest(boolean isQuery){
		if(isQuery){
			return new DataBodyQueryPower();
		}else{
			return new DataBodyQueryPower(true);
		}
	}
	
	/**查询命令*/
	public static DataBodyDevInterface queryRequest(int ApplianceType){
		switch(ApplianceType){
		case AirConditioner:
			return new DataBodyDevQuery();
		case Dehumidifier:
			return new DataBodyDeHumiQuery();
		case Humidifier:
			return new DataBodyHumiQuery();
		}
		return new DataBodyDevQuery();//默认发查询空调
	}
	
	/**向设备发送命令*/
	public static DataBodyDevInterface cmdRequest(int ApplianceType){
		switch(ApplianceType){
		case AirConditioner:
			return new DataBodyDevOld();
		case Dehumidifier:
			return new DataBodyDeHumiRequest();
		case Humidifier:
			return new DataBodyHumiRequest();
		}
		return new DataBodyDevOld();//默认空调
	}
	
	/**解析设备命令*/
	public static DataBodyDevInterface cmdResponse(int ApplianceType){
		switch(ApplianceType){
		case AirConditioner:
			return new DataResponseOld();
		case Dehumidifier:
			return new DataBodyDeHumiResponse();
		case Humidifier:
			return new DataBodyHumiResponse();
		}
		return new DataResponseOld();
	}
	
}
